﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;
namespace QldtSdh.GUI.NCS
{
    public partial class FormQuanLyKhoaHoc_ChiTiet : QldtSdh.GUI.Common.BaseDialog
    {
        KhoaHocNCS khoaHocNCS;
        int countObjectAdded = 0;
        public event Common.TListChangedEventHandler OnChanged;
        public FormQuanLyKhoaHoc_ChiTiet(KhoaHocNCS khoaHocNCS = null)
        {
            InitializeComponent();
            this.khoaHocNCS = khoaHocNCS;
            InitInterface();
        }

        void InitInterface()
        {
            if (khoaHocNCS != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.KHOAHOC);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtTen.Text = khoaHocNCS.TenKhoaHoc;
                txtNamTuyenSinh.Text = khoaHocNCS.NamTuyenSinh.ToString();
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.KHOAHOC);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtTen.Text = "";
                txtNamTuyenSinh.Text = "";
            }
            txtNamTuyenSinh.Focus();
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (txtTen.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.KHOAHOCNCS));
                txtTen.Focus();
                return false;
            }
            if (txtNamTuyenSinh.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtNamTuyenSinh, string.Format(ERROR_CONSTANT_STRING.LOI_FIELD_EMPTY));
                txtNamTuyenSinh.Focus();
                return false;
            }
            int i = 0;
            if (!int.TryParse(txtNamTuyenSinh.Text, out i))
            {
                errorNotify.SetError(txtNamTuyenSinh, string.Format(ERROR_CONSTANT_STRING.LOI_NUMBER_FORMAT));
                txtNamTuyenSinh.Focus();
                return false;
            }
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (khoaHocNCS != null)
                {
                    #region Cập nhật thông tin
                    khoaHocNCS.TenKhoaHoc = txtTen.Text;
                    khoaHocNCS.NamTuyenSinh = int.Parse(txtNamTuyenSinh.Text);
                    var rs = BizKhoaHocNCS.Update(khoaHocNCS);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.KHOAHOCNCS));
                        txtTen.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtNamTuyenSinh, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.KHOAHOCNCS));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.KHOAHOCNCS));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới
                    khoaHocNCS = new KhoaHocNCS();
                    khoaHocNCS.TenKhoaHoc = txtTen.Text;
                    khoaHocNCS.NamTuyenSinh = int.Parse(txtNamTuyenSinh.Text);
                    var rs = BizKhoaHocNCS.Add(khoaHocNCS);
                    khoaHocNCS = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        countObjectAdded++;
                        OnChanged(this, null);
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.KHOAHOCNCS));
                        txtTen.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtNamTuyenSinh, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.KHOAHOCNCS));
                        txtNamTuyenSinh.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.KHOAHOCNCS));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyHocPhan_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (countObjectAdded > 0)
                DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void txtNamTuyenSinh_Validating(object sender, CancelEventArgs e)
        {
            int i = 0;
            if (!string.IsNullOrEmpty(txtNamTuyenSinh.Text))
                if (!int.TryParse(txtNamTuyenSinh.Text, out i))
                    e.Cancel = true;
        }

        private void txtNamTuyenSinh_Validated(object sender, EventArgs e)
        {
            int i = 0;
            if (int.TryParse(txtNamTuyenSinh.Text, out i))
            {
                int nts = int.Parse(txtNamTuyenSinh.Text);
                txtTen.Text = "Khóa " + nts + " - " + (nts + 3);
            }
        }
    }
}
